package com.search.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.mysql.cj.util.StringUtils;
import com.search.service.BorrowBookService;
import com.search.service.DoubanBookCapitalService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Primary
public class DoubanTagCapitalService extends DoubanBookCapitalService {

    @Autowired
    private BorrowBookService borrowBookService;

    @Override
    public String getListIdfromUrl(String url) {
        int index = url.indexOf('g');
        String listId = url.substring(index+2,url.length());
        if(listId.endsWith("/")){
            listId.replace("/","");
        }
        return listId;
    }

    @Override
    public String dealUrl(String url,int bookSum) {
        return url+"/?start="+bookSum+"&type=S";
    }

    @Override
    public Integer bookSumIncrease(Integer bookSum) {
        return bookSum+20;
    }

    @Override
    public List<JSONObject> dealPageData(Document doc) {

        List<JSONObject> resultList = new ArrayList<>();

        try{
            Elements newsHeadlines = doc.getElementsByClass("subject-item");
            currentPageBookSum.set(newsHeadlines.size());
            if(currentPageBookSum.get()==0){
                isEmpty.set(true);
                log.info(Thread.currentThread().getName()+"页面空了---------------------------------------------------");
            }
            for (Element headline : newsHeadlines) {

                Elements elements = headline.select("h2 a");
                String detailUrl = null;
                String name = null;
                if(elements.size()>0){
                    Element element = elements.get(0);
                    detailUrl = element.attr("href");
                    name =  element.attr("title");
                    if(element.children().size()>0){
                        String subName = element.child(0).text();
                        name += subName;
                    }
                }

                //评分
                float rating = 0.0f;
                Elements ratings = headline.getElementsByClass("rating_nums");
                if(ratings.size()>0){
                    String ratingStr = ratings.get(0).text();
                    if(!StringUtils.isNullOrEmpty(ratingStr)){
                        rating = Float.parseFloat(ratingStr.trim());
                    }
                }

                JSONObject ele = new JSONObject();
                ele.put("detailUrl",detailUrl);
                ele.put("rating",rating);

                resultList.add(ele);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return resultList;
    }

}
